﻿using Medlcalcare.MVC.Common;
using Medlcalcare.MVC.Model;
using Medlcalcare.MVC.Servicesa;
using Microsoft.AspNetCore.Mvc;

namespace Medlcalcare.MVC.Controllers
{
    public class DiagnosisController : Controller
    {
        private readonly IDeepSeekServicea _deepSeekService;
        // 通过依赖注入获取服务实例
        public DiagnosisController(IDeepSeekServicea deepSeekService)
        {
            _deepSeekService = deepSeekService;
        }
        /// <summary>
        /// 问诊页面入口
        /// GET /Diagnosis
        /// </summary>
        [HttpGet]
        public IActionResult Index()
        {
            // 返回空表单视图
            return View();
        }
        /// <summary>
        /// 处理诊断请求
        /// POST /Diagnosis/GetDiagnosis
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken] // 防止CSRF攻击
        public async Task<IActionResult> GetDiagnosis(DiagnosisRequest request)
        {
            // 模型状态验证
            if (!ModelState.IsValid)
            {
                // 验证失败时返回原始表单并显示错误
                return View("Index", request);
            }
            try
            {
                // 调用服务层获取诊断结果
                var result = await _deepSeekService.GetDiagnosisSuggestionAsync(request);

                // 跳转到结果展示页面
                return View("/Views/Diagnosis/DiagnosisResult.cshtml", result);
            }
            catch (MedicalAIException ex)
            {
                // 处理业务异常
                ModelState.AddModelError("", "系统暂时无法处理您的请求，请稍后再试");
                return View("Index", request);
            }
        }
        [HttpGet]
        public IActionResult DiagnosisResult()
        {
            return View();
        }
    }
}
